<template>
  <div class="company-manage">
    <!-- 头部导航 -->
    <div class="header-nav">
      <div class="nav-tabs">
        <div class="tab active">公司名称</div>
        <div class="tab">溯源管理</div>
        <div class="tab current">企业管理</div>
        <div class="tab">平台设置</div>
        <div class="tab">我的管理</div>
      </div>
      <div class="header-actions">
        <el-button type="primary" size="small">导出</el-button>
        <el-button type="primary" size="small">导入</el-button>
        <el-button type="primary" size="small">导出模板</el-button>
      </div>
    </div>

    <div class="content-wrapper">
      <!-- 左侧企业列表 -->
      <div class="left-panel">
        <div class="panel-header">
          <h3>企业管理</h3>
          <span class="year-selector">2021-07</span>
        </div>
        
        <div class="search-section">
          <div class="search-row">
            <label>溯源管理</label>
            <el-input v-model="searchForm.traceabilityId" placeholder="请输入"></el-input>
          </div>
        </div>

        <div class="company-list">
          <div class="list-header">
            <span>序号</span>
            <span>产品</span>
            <span>溯源标识</span>
            <span>生产日期</span>
            <span>生产企业</span>
            <span>保质期</span>
            <span>操作</span>
          </div>
          
          <div 
            v-for="(item, index) in companyList" 
            :key="item.id"
            class="list-item"
            :class="{ active: selectedCompany?.id === item.id }"
            @click="selectCompany(item)"
          >
            <span>{{ index + 1 }}</span>
            <span>{{ item.product }}</span>
            <span>{{ item.traceCode }}</span>
            <span>{{ item.productionDate }}</span>
            <span>{{ item.company }}</span>
            <span>{{ item.shelfLife }}</span>
            <span class="actions">
              <el-button type="text" size="small" @click.stop="editCompany(item)">编辑</el-button>
            </span>
          </div>
        </div>

        <div class="pagination">
          <el-pagination
            v-model:current-page="currentPage"
            :page-size="pageSize"
            :total="total"
            layout="prev, pager, next"
            @current-change="handlePageChange"
          />
        </div>
      </div>

      <!-- 右侧详情面板 -->
      <div class="right-panel">
        <div class="detail-tabs">
          <div class="tab-item active">基本信息</div>
          <div class="tab-item">营销信息</div>
          <div class="tab-item">导出设置</div>
        </div>

        <div class="progress-indicators">
          <div class="progress-item" v-for="n in 8" :key="n">
            <div class="progress-bar">
              <div class="progress-fill" :style="{ width: '100%' }"></div>
            </div>
            <span class="progress-text">100%</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 基本信息弹窗 -->
    <el-dialog v-model="showBasicInfoDialog" title="基本信息" width="600px">
      <el-form :model="basicInfoForm" label-width="120px">
        <el-form-item label="商品名称：">
          <el-select v-model="basicInfoForm.productName" placeholder="兔头（麻辣味）">
            <el-option label="兔头（麻辣味）" value="兔头（麻辣味）"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生产日期：">
          <el-date-picker v-model="basicInfoForm.productionDate" type="date" placeholder="20210718"></el-date-picker>
        </el-form-item>
        <el-form-item label="规格型号（克）：">
          <el-input v-model="basicInfoForm.specification" placeholder="50"></el-input>
        </el-form-item>
        <el-form-item label="生产许可证号：">
          <el-input v-model="basicInfoForm.licenseNumber" placeholder="SC21320542760"></el-input>
        </el-form-item>
        <el-form-item label="生产企业：">
          <el-input v-model="basicInfoForm.company" placeholder="成都蜀安食品合成工有限公司"></el-input>
        </el-form-item>
        <el-form-item label="对接码：">
          <div class="qr-code-section">
            <div class="qr-placeholder">+</div>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showBasicInfoDialog = false">取消</el-button>
        <el-button type="primary" @click="saveBasicInfo">保存</el-button>
      </template>
    </el-dialog>

    <!-- 导出设置弹窗 -->
    <el-dialog v-model="showExportDialog" title="导出设置" width="500px">
      <el-form :model="exportForm" label-width="100px">
        <el-form-item label="导出数量：">
          <el-input v-model="exportForm.quantity" placeholder="50"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showExportDialog = false">取消</el-button>
        <el-button type="primary" @click="exportData">导出</el-button>
      </template>
    </el-dialog>

    <!-- 营销信息弹窗 -->
    <el-dialog v-model="showMarketingDialog" title="营销信息" width="600px">
      <el-form :model="marketingForm" label-width="100px">
        <el-form-item label="单价：">
          <el-input v-model="marketingForm.price" placeholder="无"></el-input>
        </el-form-item>
        <el-form-item label="单位：">
          <el-input v-model="marketingForm.unit" placeholder="无"></el-input>
        </el-form-item>
        <el-form-item label="保存天数：">
          <el-input v-model="marketingForm.shelfDays" placeholder="无"></el-input>
        </el-form-item>
        <el-form-item label="保存温度：">
          <el-input v-model="marketingForm.temperature" placeholder="+1-5℃"></el-input>
        </el-form-item>
        <el-form-item label="保存湿度：">
          <el-input v-model="marketingForm.humidity" placeholder="无"></el-input>
        </el-form-item>
        <el-form-item label="保存日期：">
          <el-date-picker v-model="marketingForm.storageDate" type="date" placeholder="2021-06-18"></el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showMarketingDialog = false">取消</el-button>
        <el-button type="primary" @click="saveMarketingInfo">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

// 数据定义
const searchForm = ref({
  traceabilityId: ''
})

const companyList = ref([
  {
    id: 1,
    product: '兔头（小）',
    traceCode: '20210718',
    productionDate: '20210718',
    company: '成都蜀安食品合成工有限公司',
    shelfLife: '100%'
  },
  {
    id: 2,
    product: '兔头（大）',
    traceCode: 'SC21320542760',
    productionDate: '20210718',
    company: '成都蜀安食品合成工有限公司',
    shelfLife: '60%'
  },
  {
    id: 3,
    product: '兔肚子',
    traceCode: 'SC21320542760',
    productionDate: '20210718',
    company: '成都蜀安食品合成工有限公司',
    shelfLife: '100%'
  },
  {
    id: 4,
    product: '兔肚子',
    traceCode: 'SC21320542760',
    productionDate: '20210718',
    company: '成都蜀安食品合成工有限公司',
    shelfLife: '100%'
  },
  {
    id: 5,
    product: '冷兔条',
    traceCode: 'SC21320542760',
    productionDate: '20210718',
    company: '成都蜀安食品合成工有限公司',
    shelfLife: '100%'
  },
  {
    id: 6,
    product: '兔头（麻辣）',
    traceCode: 'SC21320542760',
    productionDate: '20210718',
    company: '成都蜀安食品合成工有限公司',
    shelfLife: '100%'
  },
  {
    id: 7,
    product: '兔头（微辣）',
    traceCode: 'SC21320542760',
    productionDate: '20210718',
    company: '成都蜀安食品合成工有限公司',
    shelfLife: '100%'
  },
  {
    id: 8,
    product: '兔肚子',
    traceCode: 'SC21320542760',
    productionDate: '20210718',
    company: '成都蜀安食品合成工有限公司',
    shelfLife: '100%'
  },
  {
    id: 9,
    product: '兔肚子',
    traceCode: 'SC21320542760',
    productionDate: '20210718',
    company: '成都蜀安食品合成工有限公司',
    shelfLife: '100%'
  },
  {
    id: 10,
    product: '兔肚子',
    traceCode: 'SC21320542760',
    productionDate: '20210718',
    company: '成都蜀安食品合成工有限公司',
    shelfLife: '100%'
  }
])

const selectedCompany = ref(null)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 弹窗控制
const showBasicInfoDialog = ref(false)
const showExportDialog = ref(false)
const showMarketingDialog = ref(false)

// 表单数据
const basicInfoForm = ref({
  productName: '',
  productionDate: '',
  specification: '',
  licenseNumber: '',
  company: ''
})

const exportForm = ref({
  quantity: ''
})

const marketingForm = ref({
  price: '',
  unit: '',
  shelfDays: '',
  temperature: '',
  humidity: '',
  storageDate: ''
})

// 方法
const selectCompany = (company: any) => {
  selectedCompany.value = company
}

const editCompany = (company: any) => {
  selectedCompany.value = company
  showBasicInfoDialog.value = true
  // 填充表单数据
  basicInfoForm.value = {
    productName: company.product,
    productionDate: company.productionDate,
    specification: '50',
    licenseNumber: company.traceCode,
    company: company.company
  }
}

const handlePageChange = (page: number) => {
  currentPage.value = page
  // 这里可以调用API获取数据
}

const saveBasicInfo = () => {
  ElMessage.success('保存成功')
  showBasicInfoDialog.value = false
}

const exportData = () => {
  ElMessage.success('导出成功')
  showExportDialog.value = false
}

const saveMarketingInfo = () => {
  ElMessage.success('保存成功')
  showMarketingDialog.value = false
}

onMounted(() => {
  // 初始化数据
})
</script>

<style scoped>
.company-manage {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #f5f5f5;
}

.header-nav {
  background: white;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #e6e6e6;
}

.nav-tabs {
  display: flex;
  gap: 30px;
}

.tab {
  padding: 15px 0;
  cursor: pointer;
  position: relative;
}

.tab.active {
  color: #409eff;
}

.tab.current {
  background: #409eff;
  color: white;
  padding: 8px 16px;
  border-radius: 4px;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.content-wrapper {
  flex: 1;
  display: flex;
  gap: 20px;
  padding: 20px;
}

.left-panel {
  width: 70%;
  background: white;
  border-radius: 8px;
  padding: 20px;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.panel-header h3 {
  margin: 0;
  color: #333;
}

.year-selector {
  color: #666;
  font-size: 14px;
}

.search-section {
  margin-bottom: 20px;
}

.search-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.search-row label {
  min-width: 80px;
  font-size: 14px;
}

.company-list {
  border: 1px solid #e6e6e6;
  border-radius: 4px;
}

.list-header {
  display: grid;
  grid-template-columns: 60px 1fr 1fr 1fr 2fr 80px 80px;
  gap: 10px;
  padding: 12px;
  background: #f8f9fa;
  font-weight: 600;
  border-bottom: 1px solid #e6e6e6;
}

.list-item {
  display: grid;
  grid-template-columns: 60px 1fr 1fr 1fr 2fr 80px 80px;
  gap: 10px;
  padding: 12px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: background-color 0.2s;
}

.list-item:hover {
  background: #f8f9fa;
}

.list-item.active {
  background: #e8f4fd;
}

.actions {
  display: flex;
  gap: 8px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.right-panel {
  width: 30%;
  background: white;
  border-radius: 8px;
  padding: 20px;
}

.detail-tabs {
  display: flex;
  border-bottom: 1px solid #e6e6e6;
  margin-bottom: 20px;
}

.tab-item {
  padding: 12px 20px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
}

.tab-item.active {
  color: #409eff;
  border-bottom-color: #409eff;
}

.progress-indicators {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.progress-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.progress-bar {
  flex: 1;
  height: 8px;
  background: #f0f0f0;
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #67c23a 0%, #85ce61 100%);
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 12px;
  color: #666;
  min-width: 40px;
}

.qr-code-section {
  display: flex;
  align-items: center;
  gap: 10px;
}

.qr-placeholder {
  width: 60px;
  height: 60px;
  border: 2px dashed #ccc;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: #ccc;
  cursor: pointer;
}

.qr-placeholder:hover {
  border-color: #409eff;
  color: #409eff;
}
</style>


